home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / librw / RWTValVirtualArray.z / RWTValVirtualArray
Encoding:
Text File  |  1998-10-30  |  14.2 KB  |  331 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))                              RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWTValVirtualArray<T> - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               #include <rw/tvrtarry.h>
  13.  
  14.  
  15.  
  16.               RWVirtualPageHeap* heap;
  17.           RWTValVirtualArray<T> array(1000L, heap);
  18.  
  19.  
  20.  
  21.  
  22. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  23.      This class represents a virtual array of elements of type TTTT of almost any
  24.      length.  Individual elements are brought into physical memory as needed
  25.      basis.  If an element is updated it is automatically marked as "dirty"
  26.      and will be rewritten to the swapping medium. The swap space is provided
  27.      by an abstract page heap which is specified by the constructor.  Any
  28.      number of virtual arrays can use the same abstract page heap.  YYYYoooouuuu mmmmuuuusssstttt
  29.      ttttaaaakkkkeeee ccccaaaarrrreeee tttthhhhaaaatttt tttthhhheeee ddddeeeessssttttrrrruuuuccccttttoooorrrr ooooffff tttthhhheeee aaaabbbbssssttttrrrraaaacccctttt ppppaaaaggggeeee hhhheeeeaaaapppp iiiissss nnnnooootttt ccccaaaalllllllleeeedddd
  30.      bbbbeeeeffffoooorrrreeee aaaallllllll vvvviiiirrrrttttuuuuaaaallll aaaarrrrrrrraaaayyyyssss bbbbuuuuiiiilllltttt ffffrrrroooommmm iiiitttt hhhhaaaavvvveeee bbbbeeeeeeeennnn ddddeeeessssttttrrrrooooyyyyeeeedddd....  The class
  31.      supports reference counting using a copy-on-write technique, so (for
  32.      example) returning a virtual array by value from a function is as
  33.      efficient as it can be.  Be aware, however, that if the copy-on-write
  34.      machinery finds that a copy must ultimately be made, then for large
  35.      arrays this could take quite a bit of time.  For efficiency, more than
  36.      one element can (and should) be put on a page.  The actual number of
  37.      elements is equal to the page size divided by the element size, rounded
  38.      downwards.  Example: for a page size of 512 bytes, and an element size of
  39.      8, then 64 elements would be put on a page.  The indexing operator
  40.      (ooooppppeeeerrrraaaattttoooorrrr[[[[]]]]((((lllloooonnnngggg))))) actually returns an object of type
  41.      RRRRWWWWTTTTVVVViiiirrrrttttuuuuaaaallllEEEElllleeeemmmmeeeennnntttt<<<<TTTT>>>>....  Consider this example:
  42.  
  43.               double d = vec[j];
  44.  
  45.  
  46.  
  47.               vec[i] = 22.0;
  48.  
  49.  
  50.      Assume that vvvveeeecccc is of type RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy<<<<ddddoooouuuubbbblllleeee>>>>....  The expression
  51.      vvvveeeecccc[[[[jjjj]]]] will return an object of type RRRRWWWWTTTTVVVViiiirrrrttttuuuuaaaallllEEEElllleeeemmmmeeeennnntttt<<<<ddddoooouuuubbbblllleeee>>>>,,,, which
  52.      will contain a reference to the element being addressed.  In the first
  53.      line, this expression is being used to initialize a ddddoooouuuubbbblllleeee.  The class
  54.      RRRRWWWWTTTTVVVViiiirrrrttttuuuuaaaallllEEEElllleeeemmmmeeeennnntttt<<<<TTTT>>>> contains a type conversion operator to convert
  55.      itself to a TTTT, in this case a double.  The compiler uses this to
  56.      initialize dddd in the first line.  In the second line, the expression
  57.      vvvveeeecccc[[[[iiii]]]] is being used as an lvalue.  In this case, the compiler uses the
  58.      assignment operator for RRRRWWWWTTTTVVVViiiirrrrttttuuuuaaaallllEEEElllleeeemmmmeeeennnntttt<<<<TTTT>>>>....  This assignment operator
  59.      recognizes that the expression is being used as an lvalue and
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))                              RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74.      automatically marks the appropriate page as "dirty," thus guaranteeing
  75.      that it will be written back out to the swapping medium.  Slices, as well
  76.      as individual elements, can also be addressed.  These should be used
  77.      wherever possible as they are much more efficient because they allow a
  78.      page to be locked and used multiple times before unlocking.  The class TTTT
  79.      must have:
  80.           well-defined copy semantics (TTTT::::::::TTTT((((ccccoooonnnnsssstttt TTTT&&&&)))) or equiv.);
  81.  
  82.           well-defined assignment semantics (TTTT::::::::ooooppppeeeerrrraaaattttoooorrrr====((((ccccoooonnnnsssstttt TTTT&&&&)))) or
  83.           equiv.).
  84.  
  85.  
  86. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  87.      In addition, you must never take the address of an element. None
  88.  
  89. EEEExxxxaaaammmmpppplllleeee
  90.      In this example, a virtual vector of objects of type EEEErrrrssssaaaattttzzzzIIIInnnntttt is
  91.      exercised.  A disk-based page heap is used for swapping space.
  92.  
  93.               #include <rw/tvrtarry.h>
  94.           #include <rw/rstream.h>
  95.           #include <rw/diskpage.h>
  96.           #include <stdlib.h>
  97.           #include <stdio.h>
  98.           struct ErsatzInt {
  99.             char  buf[8];
  100.             ErsatzInt(int i) { sprintf(buf, "%d", i); }
  101.             friend ostream& operator<<(ostream& str, ErsatzInt& i)
  102.               { str << atoi(i.buf); return str; }
  103.           };
  104.           main() {
  105.             RWDiskPageHeap heap;
  106.             RWTValVirtualArray<ErsatzInt> vec1(10000L, &heap);
  107.             for (long i=0; i<10000L; i++)
  108.               vec1[i] = i;     // Some compilers may need a cast here
  109.             cout << vec1[100] << endl;     // Prints "100"
  110.             cout << vec1[300] << endl;     // Prints "300"
  111.             RWTValVirtualArray<ErsatzInt> vec2 = vec1.slice(5000L, 500L);
  112.             cout << vec2.length() << endl;     // Prints "500"
  113.             cout << vec2[0] << endl;     // Prints "5000";
  114.             return 0;
  115.           }
  116.  
  117.  
  118.      Program output:
  119.  
  120.               100
  121.           300
  122.           500
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))                              RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  141.      5000
  142.  
  143.  
  144.  
  145.               RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy<<<<TTTT>>>>(long size, RWVirtualPageHeap* heap);
  146.  
  147.  
  148.      Construct a vector of length ssssiiiizzzzeeee.  The pages for the vector will be
  149.      allocated from the page heap given by hhhheeeeaaaapppp which can be of any type.
  150.  
  151.               RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy<<<<TTTT>>>>(const RWTValVirtualArray<T>& v);
  152.  
  153.  
  154.      Constructs a vector as a copy of vvvv.  The resultant vector will use the
  155.      same heap and have the same length as vvvv.  The actual copy will not be
  156.      made until a write, minimizing the amount of heap allocations and copying
  157.      that must be done.
  158.  
  159.               RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy<<<<TTTT>>>>(const RWTVirtualSlice<T>& sl);
  160.  
  161.  
  162.      Constructs a vector from a slice of another vector.  The resultant vector
  163.      will use the same heap as the vector whose slice is being taken.  Its
  164.      length will be given by the length of the slice.  The copy will be made
  165.      immediately.
  166.  
  167. PPPPuuuubbbblllliiiicccc DDDDeeeessssttttrrrruuuuccccttttoooorrrr
  168.               ~~~~RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy<<<<TTTT>>>>();
  169.  
  170.  
  171.      Releases all pages allocated by the vector.
  172.  
  173.  
  174.  
  175.  
  176.  
  177. PPPPuuuubbbblllliiiicccc OOOOppppeeeerrrraaaattttoooorrrrssss
  178.               RWTValVirtualArray&
  179.           ooooppppeeeerrrraaaattttoooorrrr====(const RWTValVirtualArray<T>& v);
  180.  
  181.  
  182.      Sets self to a copy of vvvv.  The resultant vector will use the same heap
  183.      and have the same length as vvvv.  The actual copy will not be made until a
  184.      write, minimizing the amount of heap allocations and copying that must be
  185.      done.
  186.  
  187.               void
  188.           ooooppppeeeerrrraaaattttoooorrrr====(const RWTVirtualSlice<T>& sl);
  189.  
  190.  
  191.      Sets self equal to a sssslllliiiicccceeee of another vector.  The resultant vector will
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))                              RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.      use the same heap as the vector whose slice is being taken.  Its length
  207.      will be given by the length of the slice.  The copy will be made
  208.      immediately.
  209.  
  210.               T
  211.           ooooppppeeeerrrraaaattttoooorrrr====(const T& val);
  212.  
  213.  
  214.      Sets all elements in self equal to vvvvaaaallll.  This operator is actually quite
  215.      efficient because it can work with many elements on a single page at
  216.      once.  A copy of vvvvaaaallll is returned.
  217.  
  218.               T
  219.           ooooppppeeeerrrraaaattttoooorrrr[[[[]]]](long i) const;
  220.  
  221.  
  222.      Returns a copy of the value at index iiii.  The index iiii must be between zero
  223.      and the length of the vector less one or an exception of type
  224.      TTTTOOOOOOOOLLLL____LLLLOOOONNNNGGGGIIIINNNNDDDDEEEEXXXX will occur.
  225.  
  226.               RWTVirtualElement<T>
  227.           ooooppppeeeerrrraaaattttoooorrrr[[[[]]]](long);
  228.  
  229.  
  230.      Returns a reference to the value at index iiii.  The results can be used as
  231.      an lvalue.  The index iiii must be between zero and the length of the vector
  232.      less one or an exception of type TTTTOOOOOOOOLLLL____LLLLOOOONNNNGGGGIIIINNNNDDDDEEEEXXXX will occur.
  233.  
  234. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss
  235.               long
  236.           lllleeeennnnggggtttthhhh() const;
  237.  
  238.  
  239.      Returns the length of the vector.
  240.  
  241.               T
  242.           vvvvaaaallll(long i) const;
  243.  
  244.  
  245.      Returns a copy of the value at index iiii.  The index iiii must be between zero
  246.      and the length of the vector less one or an exception of type
  247.      TTTTOOOOOOOOLLLL____LLLLOOOONNNNGGGGIIIINNNNDDDDEEEEXXXX will occur.
  248.  
  249.               void
  250.           sssseeeetttt(long i, const T& v);
  251.  
  252.  
  253.      Sets the value at the index iiii to vvvv.  The index iiii must be between zero and
  254.      the length of the vector less one or an exception of type TTTTOOOOOOOOLLLL____LLLLOOOONNNNGGGGIIIINNNNDDDDEEEEXXXX
  255.      will occur.
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))                              RRRRWWWWTTTTVVVVaaaallllVVVViiiirrrrttttuuuuaaaallllAAAArrrrrrrraaaayyyy((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.               RWTVirtualSlice<T>
  273.           sssslllliiiicccceeee(long start, long length);
  274.  
  275.  
  276.      Returns a reference to a sssslllliiiicccceeee of self.  The value ssssttttaaaarrrrtttt is the starting
  277.      index of the slice, the value lllleeeennnnggggtttthhhh its extent.  The results can be used
  278.      as an lvalue.
  279.  
  280.               void
  281.           rrrreeeesssshhhhaaaappppeeee(long newLength);
  282.  
  283.  
  284.      Change the length of the vector to nnnneeeewwwwLLLLeeeennnnggggtttthhhh.  If this results in the
  285.      vector being lengthened then the value of the new elements is undefined.
  286.  
  287.               RWVirtualPageHeap*
  288.           hhhheeeeaaaapppp() const;
  289.  
  290.  
  291.      Returns a pointer to the heap from which the vector is getting its pages.
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.